//
//  TestPlanCell.m
//  IpadTestB
//
//  Created by edianzu on 2017/8/3.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "TestPlanCell.h"
@interface TestPlanCell()
@property(nonatomic,assign)NSInteger  integerS;
@end
@implementation TestPlanCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatUI];
    }
    self.selectionStyle =UITableViewCellSelectionStyleNone;
    return self;
}

-(void) creatUI{
    self.contentView.backgroundColor =UIColorFromRGB(241, 244, 248);
    self.topLine = [[UIView alloc]init];
    self.topLine.backgroundColor = UIColorFromRGB(208, 63, 0);
    [self.contentView addSubview:self.topLine];
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).with.offset(20*AutoScaleHorizontal);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.size.mas_equalTo(CGSizeMake(3*AutoScaleHorizontal, (65/2)*AutoScaleVertical));
    }];
    
    self.redCircle = [[UIImageView alloc]init];
    self.redCircle.image = [UIImage imageNamed:@"redCircle"];
    [self.contentView addSubview:self.redCircle];
    [self.redCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topLine.mas_centerX);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.bottomLine = [[UIView alloc]init];
    self.bottomLine.backgroundColor = UIColorFromRGB(208, 63, 0);
    [self.contentView addSubview:self.bottomLine];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).with.offset(20*AutoScaleHorizontal);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(3*AutoScaleHorizontal, (65/2)*AutoScaleVertical));
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font =[UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.timeLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.redCircle.mas_right).with.offset(10*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.redCircle.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(130*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];
    
    self.rightBackView = [[UIView alloc]init];
    self.rightBackView.backgroundColor = [UIColor whiteColor];
    self.rightBackView.layer.cornerRadius = 10;
    [self.contentView addSubview:self.rightBackView];
    [self.rightBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.timeLabel.mas_right).with.offset(2*AutoScaleHorizontal);
        make.top.mas_equalTo(self.contentView.mas_top).with.offset(5*AutoScaleVertical);
        make.right.mas_equalTo(self.contentView.mas_right);
        //        make.size.mas_equalTo(CGSizeMake(650*AutoScaleHorizontal, 75*AutoScaleVertical));
        make.height.mas_equalTo(75*AutoScaleVertical);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    [self.rightBackView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightBackView.mas_left).with.offset(35*AutoScaleHorizontal);
        make.top.mas_equalTo(self.rightBackView.mas_top).with.offset(15*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(270*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.textColor = [UIColor blackColor];
    self.numberLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.rightBackView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightBackView.mas_left).with.offset(35*AutoScaleHorizontal);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(12*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(270*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.teacherLabel = [[UILabel alloc]init];
    self.teacherLabel.textColor = [UIColor blackColor];
    self.teacherLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.rightBackView addSubview:self.teacherLabel];
    [self.teacherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel.mas_right).with.offset(20*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(165*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.doingLabel = [[UILabel alloc]init];
    self.doingLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.rightBackView addSubview:self.doingLabel];
    [self.doingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.teacherLabel.mas_right);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editBtn addTarget:self action:@selector(bringEditView) forControlEvents:UIControlEventTouchUpInside];
    [self.editBtn setImage:[UIImage imageNamed:@"editimg"] forState:UIControlStateNormal];
    [self.rightBackView addSubview:self.editBtn];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.doingLabel.mas_right).with.offset(59*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(70*AutoScaleHorizontal, 75*AutoScaleVertical));
    }];
    
    
    
//    self.editImgView.hidden =YES;
//    self.arrowImg = [[UIImageView alloc]init];
//    self.arrowImg.image = [UIImage imageNamed:@"arrowImage"];
//    [self.rightBackView addSubview:self.arrowImg];
//    [self.arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.doingLabel.mas_right);
//        make.centerY.mas_equalTo(self.teacherLabel.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(10*AutoScaleHorizontal, 15*AutoScaleVertical));
//    }];
    
}

-(void)relayoutCellWithModel:(PlanModel *)model andIndex:(NSInteger)index andArrCount:(NSInteger)count andJoinNum:(NSNumber *)num{
    self.integerS = index;
    if (index == 0) {
        self.topLine.hidden =YES;
    }
    if (index == count-1) {
        self.bottomLine.hidden =YES;
    }
    self.timeLabel.text = [model.CreateTime substringWithRange:NSMakeRange(0, 10)];
    
    self.titleLabel.text = model.Title;
    
    self.numberLabel.text = [NSString stringWithFormat:@"%@人参加",num];
    
    self.teacherLabel.text = [NSString stringWithFormat:@"教师:%@",model.CreateTeacher];
    
    
    if (model.Status ==YES) {
        self.doingLabel.textColor = UIColorFromRGB(0, 170, 160);
        self.doingLabel.text = @"测试完";
    }else{
        self.doingLabel.text = @"进行中";
        self.doingLabel.textColor = [UIColor redColor];
    }
}

#pragma mark 按钮方法
-(void)bringEditView{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    self.backView = [[UIView alloc]init];
    self.backView.backgroundColor = [UIColor clearColor];
    self.backView.userInteractionEnabled =YES;
    [window addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(window);
    }];
    [window bringSubviewToFront:self.backView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moveOut)];
    [self.backView addGestureRecognizer:tap];
    
    
    self.editImgView =[[ UIImageView alloc]init];
    self.editImgView.userInteractionEnabled = YES;
    self.editImgView.backgroundColor = [UIColor clearColor];
    self.editImgView.image = [UIImage imageNamed:@"bigCircle"];
    [self.backView addSubview:self.editImgView];
    [self.editImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editBtn.mas_bottom).with.offset(-30*AutoScaleVertical);
        make.left.mas_equalTo(self.doingLabel.mas_left).with.offset(60*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(125*AutoScaleHorizontal, 100*AutoScaleVertical));
    }];
    
    self.editImgBtnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editImgBtnEdit addTarget:self action:@selector(bianji) forControlEvents:UIControlEventTouchUpInside];
    self.editImgBtnEdit.backgroundColor = [UIColor whiteColor];
    [self.editImgBtnEdit setImage:[UIImage imageNamed:@"editInfo"] forState:UIControlStateNormal];
    [self.editImgBtnEdit setTitle:@"编辑" forState:UIControlStateNormal];
    [self.editImgBtnEdit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.editImgView addSubview:self.editImgBtnEdit];
    [self.editImgBtnEdit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editImgView.mas_top).with.offset(20*AutoScaleVertical);
        make.left.mas_equalTo(self.editImgView.mas_left).with.offset(5*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(115*AutoScaleHorizontal, 35*AutoScaleVertical));
    }];
    
    UIView * line = [[UIView alloc]init];
    line.backgroundColor = UIColorFromRGB(236, 236, 236);
    [self.editImgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editImgBtnEdit.mas_bottom);
        make.left.mas_equalTo(self.editImgView.mas_left).with.offset(1);
        make.size.mas_equalTo(CGSizeMake(123*AutoScaleHorizontal, 1));
    }];
    
    self.editImgBtnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editImgBtnDelete addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
    self.editImgBtnDelete.backgroundColor = [UIColor whiteColor];
    [self.editImgBtnDelete setImage:[UIImage imageNamed:@"deleteIMG"] forState:UIControlStateNormal];
    [self.editImgBtnDelete setTitle:@"删除" forState:UIControlStateNormal];
    [self.editImgBtnDelete setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.editImgView addSubview:self.editImgBtnDelete];
    [self.editImgBtnDelete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editImgBtnEdit.mas_bottom).with.offset(1*AutoScaleVertical);
        make.left.mas_equalTo(self.editImgView.mas_left).with.offset(5*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(115*AutoScaleHorizontal, 35*AutoScaleVertical));
    }];
    
}

-(void)moveOut{
    [self.backView removeFromSuperview];
}


-(void)bianji{
    NSLog(@"bianji");
    [self.backView removeFromSuperview];
    self.callEdit(self.integerS);
}


-(void)shanchu{
    NSLog(@"shanchu");
    [self.backView removeFromSuperview];
    self.callDelete(self.integerS);
}

@end
